//
//  HMRefreshControl.swift
//  Faner_Weibo
//
//  Created by 范英强 on 16/2/12.
//  Copyright © 2016年 范英强. All rights reserved.
//

import UIKit

private let refreshH: CGFloat = 50

enum HMRefreshControlStatus:Int {
    case Nolmal = 0
    case Pulling = 1
    case Refreshing = 2
}
class HMRefreshControl: UIControl {
    
    var scrollView: UIScrollView?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        backgroundColor = UIColor.redColor()
        var frame = self.frame
        frame.size = CGSize(width: SCREENW, height: refreshH)
        frame.origin.y = -refreshH
        self.frame = frame
    }
    
    override func willMoveToSuperview(newSuperview: UIView?) {
        if let srcollView = newSuperview as? UIScrollView {
            srcollView.addObserver(self, forKeyPath: "contentOffset", options: NSKeyValueObservingOptions.New, context:nil)
            self.scrollView = srcollView as!UIScrollView
        }
    }
    
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        print(change)
    }
    
    deinit{
        scrollView?.removeObserver(self, forKeyPath: "contentOffset")
    }
}
